package udpInvoker.main;

import udpInvoker.util.UdpUtils;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * Created by IntelliJ IDEA.
 * User: esr
 * Date: 22.okt.2008
 * Time: 16:07:07
 * Copyright 2008  Espen Skjervold, FFI
 */
public class UdpMulticastReceiverThread extends Thread{


    private BlockingQueue messageQueue;

    private int transportType=1;
    private String unicastAddress;
    private int port;


    public UdpMulticastReceiverThread() {
        setPriority(Thread.MAX_PRIORITY);
        messageQueue = new LinkedBlockingQueue();

    }



    public void run() {

        while (true) {

            try {
                byte[] buffer = UdpUtils.receiveMessageBuffer(transportType, unicastAddress, port, true);

                messageQueue.put(buffer);
             

            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }

    }


    public byte[] getMessageBuffer() throws Exception{

        return (byte[]) messageQueue.take(); //blocking call

    }
    
      public void setUnicastAddress(String address, int port) {
        transportType=2;
        unicastAddress=address;
        this.port=port;
    }


}
